/***************************************************************************
                          main.c  -  description
                             -------------------
    begin                : Tue Mar 22 15:06:23 BRT 2005
    copyright            : (C) 2005 by Nelson Junior
    email                : nelson@dsc.ufcg.edu.br
***************************************************************************/



#include <cstdlib>



#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TextOutputter.h>


#include <libxml/tree.h>
#include <libxml/parser.h>
#include <cstdio>

#include "simulator.h"

/*|----Main function----|*/
int main( int argc, char **argv) {
	/*command line options*/
	char *option = NULL;
	char *heuristic_name = NULL;
	char *scenario_desc = NULL;
	char *model = NULL;
	char *converted_file = NULL;
	char *error_option = NULL;
	char *grid_percentage_unavailability = NULL;
	/*Simulator*/
	Simulator *sim;

	/*Verify the args*/
  if ( argc < 2) {
    	std:: cout << "COMMAND LINE OPTIONS\n\n";
    	std:: cout << "AdaptiveInfoScheduling --simulate <heuristic_name> <scenario_description.xml> <error | no_error> <GridPercentageUnavailability(%)>\n\n";    	
    	std:: cout << "AdaptiveInfoScheduling --makescenario  <model.xml> <resulting_scenario.xml>\n\n";    	    	
    	std:: cout << "AdaptiveInfoScheduling --tests\n\n";    	    	
    	std:: cout << "AdaptiveInfoScheduling --convert <source.xml> <similar.xml> <converted.xml>\n\n";    	    	    	
    	exit(1);
  }
  
  option = argv[1];
  heuristic_name = argv[2];
  model = argv[2];  
  scenario_desc = argv[3];
  converted_file = argv[4];
  error_option = argv[4];
  grid_percentage_unavailability = argv[5];
  
  if (error_option==NULL) error_option = "no_error";
  if (grid_percentage_unavailability==NULL) grid_percentage_unavailability = "0";

  try {
	  /*Verifying the selected option*/
	  if (!strcmp(option, "--simulate")) { /*option is simulate*/
	  	
	  	int heuristic_id = -1;
	  	
	  	if (!strcmp(heuristic_name, "STOAFF")) {
	  		heuristic_id = Scheduler::STOAFF;
	  	} else if (!strcmp(heuristic_name, "XSUFF")) {
	  		heuristic_id = Scheduler::XSUFF;
	  	} else if (!strcmp(heuristic_name, "INFOADAPT")) {
	  		heuristic_id = Scheduler::INFOADAPT;
	  	} else if (!strcmp(heuristic_name, "WQR")) {
	  		heuristic_id = Scheduler::WQR;
  		} else if (!strcmp(heuristic_name, "RUMR")) {
	  		heuristic_id = Scheduler::RUMR;
	  	} else {
	  		std:: cout  << "Invalid Heuristic!" << "\nAvailable options:  STOAFF | XSUFF | INFOADAPT | WQR | RUMR" << "\"\n";  		
	  		exit(1);
	  	}
	  	
	  	int error = -1;

	  	if (!strcmp(error_option, "error")) {
	  		error = Scheduler::WITH_ERROR;
	  	} else if (!strcmp(error_option, "no_error")) {
	  		error = Scheduler::WITHOUT_ERROR;	  		
	  	} else {
	  		std::cout << "\nInvalid Option\nAvailable Options: error | no_error";
	  	}
	  	
	  	double gridUnavailability = atof(grid_percentage_unavailability);
	  	fprintf(stderr, "\nGridunavailability=%f\n", gridUnavailability);
	  	
	  	sim = new Simulator(scenario_desc, heuristic_id, error, gridUnavailability);
	  	std:: cout  << "Simulation of scenario \"" << scenario_desc << "\"(seed="<< sim->getScenario()->getSeed() << ") using heuristic \"" << sim->getScheduler()->getHeuristic()->getName() << "\"...\n";
	  	sim->simulate();
	  	std::cout << "\nFinished the simulation of heuristic \""<< sim->getScheduler()->getHeuristic()->getName() << "\"\n";  	
	  	delete sim;
	
	
	  } else if   (!strcmp(option,"--makescenario") ) { /*option is makescenario*/
	  	
		std:: cout  << "\nGenerating a scenario from model \""<< model  << "\" into file \"" <<  scenario_desc << "\"\n"; 
		
		/*Create a scenario from model and writes it to XML*/
		Simulator::makeScenarioFromModel(model, scenario_desc);
			
		std:: cout << "\nScenario created sucessfully!\n";
		
	  } else if   (!strcmp(option,"--tests") ) { /*option is perfom tests*/
	  	
	  	CppUnit::TextUi::TestRunner runner;
	  	
	  	/*Gets the test registry*/
		CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
		
		/*Add all registred tests to the runner*/
	    runner.addTest( registry.makeTest() );
	
	    runner.setOutputter( CppUnit::CompilerOutputter::defaultOutputter( &runner.result(),  std::cerr ) );
	
	    runner.run( );
	  	
	  } else if   (!strcmp(option,"--convert") ) { /*option is perfom convert*/

		std:: cout  << "\nGenerating a scenario from \""<< model  << "\" , based on scenario \"" <<  scenario_desc << "\", into file \"" << converted_file << "\"  \n"; 
		
		/*Create a scenario from model and writes it to XML*/
		Simulator::convertScenario(model, scenario_desc, converted_file);
			
		std:: cout << "\nScenario converted sucessfully!\n";

	  } else { /*Invalid option*/
	   	std:: cout << "Invalid Option! Try the following ones:\n\n";
    	std:: cout << "AdaptiveInfoScheduling --simulate <heuristic_name> <scenario_description.xml> <error | no_error> <GridPercentageUnavailability(%)>\n\n";    	
	   	std:: cout << "AdaptiveInfoScheduling --makescenario  <model.xml> <resulting_scenario.xml>\n\n";    	    	
	   	std:: cout << "AdaptiveInfoScheduling --tests \n\n";    	    	
    	std:: cout << "AdaptiveInfoScheduling --convert <source.xml> <similar.xml> <converted.xml>\n\n";    	    	    	
	   	exit(1);
	  }

  	
  } catch (std::runtime_error &e) {
  	fprintf(stderr, "ERROR : %s ", e.what());
  	exit(1);
  }
  
 
}


